<?php
/**
 * @file name : Mscandir.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Mscandir
{
    private $dir = NULL;

    public function __construct()
    {
        $this->init();
    }
    /**
     * init
     *
	 * This function set
     */
    public function init()
    {
        $this->dir = getcwd();
    }

    public function getScanDir($dirPath = NULL, $deep = FALSE)
    {
        if ($dirPath == NULL) {
            $dirPath = $this->dir;
        }
        if ($deep == FALSE) {
            $currDir = scandir($dirPath);
            return array_diff($currDir, array('.', '..'));

        } else {
            return $this->recursive_directory_tree($dirPath);
        }
    }
    /*
     * This function mimicks the functionality of the built in scandir() function
     * but it will detect folders in the directory and scan those, too. This works
     * for as many levels of folder as you can give it.
     *
     * Sam Rose, 2010, http://lbak.co.uk
     */
    private function recursive_directory_tree($directory = null)
    {
        //declare the array to return
        $return = array();

        //Check if the argument specified is an array
        if (is_dir($directory)) {
            //Scan the directory and loop through the results
            foreach(scandir($directory) as $file) {

                //. = current directory, .. = up one level. We want to ignore both.
                if ($file == "." || $file == "..") {
                    continue;
                }

                //Check if the current $file is a directory itself.
                //The appending of $directory is necessary here.
                if (is_dir($directory."/".$file)) {
                    //Create a new array with an index of the folder name.
                    $return[$file] = self::recursive_directory_tree($directory."/".$file);
                }
                else {
                    //If $file is not a directory, just add it to th return array.
                    $return[] = $file;
                }
            }
        }
        else {
            //$directory is not a directory, just add it to the return array as a
            //file.
            $return[] = $directory;
        }

        //Return the return array.
        return $return;
    }

    // ------------ lixlpixel recursive PHP functions -------------
    // scan_directory_recursively( directory to scan, filter )
    // expects path to directory and optional an extension to filter
    // of course PHP has to have the permissions to read the directory
    // you specify and all files and folders inside this directory
    // ------------------------------------------------------------
    // to use this function to get all files and directories in an array, write:
    // $filestructure = scan_directory_recursively('path/to/directory');
    // to use this function to scan a directory and filter the results, write:
    // $fileselection = scan_directory_recursively('directory', 'extension');

    private function scan_directory_recursively($directory, $filter=FALSE)
    {
        // if the path has a slash at the end we remove it here
        if(substr($directory,-1) == '/')
        {
            $directory = substr($directory,0,-1);
        }

        // if the path is not valid or is not a directory ...
        if(!file_exists($directory) || !is_dir($directory))
        {
            // ... we return false and exit the function
            return FALSE;

        // ... else if the path is readable
        }elseif(is_readable($directory))
        {
            // initialize directory tree variable
            $directory_tree = array();

            // we open the directory
            $directory_list = opendir($directory);

            // and scan through the items inside
            while (FALSE !== ($file = readdir($directory_list)))
            {
                // if the filepointer is not the current directory
                // or the parent directory
                if($file != '.' && $file != '..')
                {
                    // we build the new path to scan
                    $path = $directory.'/'.$file;

                    // if the path is readable
                    if(is_readable($path))
                    {
                        // we split the new path by directories
                        $subdirectories = explode('/',$path);

                        // if the new path is a directory
                        if(is_dir($path))
                        {
                            // add the directory details to the file list
                            $directory_tree[] = array(
                                'path'    => $path,
                                'name'    => end($subdirectories),
                                'kind'    => 'directory',

                                // we scan the new path by calling this function
                                'content' => scan_directory_recursively($path, $filter));

                        // if the new path is a file
                        }elseif(is_file($path))
                        {
                            // get the file extension by taking everything after the last dot
                            $extension = end(explode('.',end($subdirectories)));

                            // if there is no filter set or the filter is set and matches
                            if($filter === FALSE || $filter == $extension)
                            {
                                // add the file details to the file list
                                $directory_tree[] = array(
                                    'path'      => $path,
                                    'name'      => end($subdirectories),
                                    'extension' => $extension,
                                    'size'      => filesize($path),
                                    'kind'      => 'file');
                            }
                        }
                   }
                }
            }
            // close the directory
            closedir($directory_list);

            // return file list
            return $directory_tree;

        // if the path is not readable ...
        }else{
            // ... we return false
            return FALSE;
        }
    }
}